package com.aikg.kgrag_java.controller;

import com.aikg.kgrag_java.pojo.entity.Question;
import com.aikg.kgrag_java.service.QuestionService;
import com.aikg.kgrag_java.utils.Result;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @Author tfan
 * @Description
 * @Date 2025/1/2 20:39
 **/
@RestController
@RequestMapping("/question")
public class QuestionController {

    @Resource
    private QuestionService questionService;

    private static final Integer SUGGESTION_COUNT = 6;

    /**
     * 保存问题
     *
     * @param questions
     * @param projectId
     * @return
     */
    @GetMapping("/save")
    public Result saveQuestion(@RequestParam(name = "text") List<String> questions,
                               @RequestParam(name = "projectId", defaultValue = "") Integer projectId) {
//        List<Question> questionList = questions.stream().map(q -> {
//            return new Question(UUID.randomUUID().toString().replace("-", ""), q, 1, "" + projectId);
//        }).collect(Collectors.toList());
//        questionService.saveBatch(questionList);
        // TODO 后续改为批量保存
        for (String question : questions) {
            questionService.saveOrUpdateOne(new Question(UUID.randomUUID().toString().replace("-", ""), question, 1, "" + projectId, null));
        }
        return Result.buildSuccessResult();
    }

    /**
     * 检查问题是否需要入库
     *
     * @param text
     * @param keyword
     * @return
     */
    @GetMapping("/check")
    public Result checkKeyword(@RequestParam(name = "text") String text,
                               @RequestParam(name = "keyword") String keyword) {
        return Result.buildSuccessResultWithData(true);


    }

    /**
     * 推荐问题
     *
     * @param text
     * @param count
     * @param projectId
     * @return
     */
    @PostMapping("/recommend")
    public Result recommend(@RequestParam(name = "text", defaultValue = "") String text,
                            @RequestParam(name = "count", defaultValue = "20") String count,
                            @RequestParam(name = "projectId", defaultValue = "") Integer projectId) {
        if (text == null || text.trim().isEmpty()) {
            List<Question> questions = questionService.getTopList(Integer.parseInt(count), String.valueOf(projectId));
            return Result.buildSuccessResultWithData(questions);
        } else {
            List<Question> questions = questionService.getByKeywords(text, 1, Integer.parseInt(count), String.valueOf(projectId));
            return Result.buildSuccessResultWithData(questions);
        }
    }

    @GetMapping("/suggest")
    public Result suggest(@RequestParam(name = "text", defaultValue = "") String text, @RequestParam(name = "projectId", defaultValue = "0") String projectId) {
        List<Question> suggestions = questionService.getByKeywords(text, 1, SUGGESTION_COUNT, projectId);
        return Result.buildSuccessResultWithData(suggestions);
    }

    /**
     * 获取热门问题
     *
     * @param projectId
     * @return
     */
    @GetMapping("/hotpot")
    public Result getHotpot(@RequestParam(name = "projectId", defaultValue = "0") String projectId,
                            @RequestParam(name = "startDateTime", defaultValue = "0") LocalDateTime startDateTime,
                            @RequestParam(name = "endDateTime", defaultValue = "0") LocalDateTime endDateTime) {
        List<Question> suggestions = questionService.getHotQuestions(startDateTime, endDateTime, 100, projectId);
        return Result.buildSuccessResultWithData(suggestions);
    }

    @GetMapping("/search")
    public Result search(@RequestParam(name = "key", defaultValue = "0") String key, @RequestParam(name = "projectId", defaultValue = "0") String projectId) {
        List<Question> suggestions = questionService.getByKeyword(key, projectId);
        return Result.buildSuccessResultWithData(suggestions);
    }

    @GetMapping("/delete")
    public Result delete(@RequestParam(name = "id", defaultValue = "0") String id) {
        questionService.deleteOne(id);
        return Result.buildSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Question question) {
        questionService.updateOne(question);
        return Result.buildSuccessResult();
    }

    @PostMapping("/saveOne")
    public Result save(@RequestBody Question question) {

        question.setId(UUID.randomUUID().toString().replace("-", ""));
        question.setFrequency(1);
        question.setLastViewedDateStr(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        questionService.save(question);
        return Result.buildSuccessResult();
    }
}
